package com.robot.console.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class Zip
{
	public static byte[] compressBytesByGZIP(byte[] b)
	{
		ByteArrayOutputStream baos = null;
		GZIPOutputStream gzos = null;
		try
		{
			baos = new ByteArrayOutputStream();
			gzos = new GZIPOutputStream(baos);
			gzos.write(b);
			gzos.finish();
			byte[] b1 = baos.toByteArray();
			return b1;
		}
		catch (Exception ex)
		{
			return null;
		}
		finally
		{
			if (gzos != null)
			{
				try
				{
					gzos.close();
				}
				catch (Exception e)
				{
				}
			}
			if (baos != null)
			{
				try
				{
					baos.close();
				}
				catch (Exception e)
				{
				}
			}
		}
	}

	public static byte[] compressStringByZIP(String str)
	{
		if (str == null)
			return null;
		byte[] compressed;
		ByteArrayOutputStream out = null;
		ZipOutputStream zout = null;
		try
		{
			out = new ByteArrayOutputStream();
			zout = new ZipOutputStream(out);
			zout.putNextEntry(new ZipEntry("0"));
			zout.write(str.getBytes());
			zout.closeEntry();
			compressed = out.toByteArray();
		}
		catch (Exception ex)
		{
			compressed = null;
		}
		finally
		{
			if (zout != null)
			{
				try
				{
					zout.close();
				}
				catch (IOException e)
				{
				}
			}
			if (out != null)
			{
				try
				{
					out.close();
				}
				catch (IOException e)
				{
				}
			}
		}
		return compressed;
	}

	public static String decompressBytesByZIP(byte[] compressed)
	{
		if (compressed == null)
		{
			return null;
		}
		ByteArrayOutputStream out = null;
		ByteArrayInputStream in = null;
		ZipInputStream zin = null;
		String decompressed;
		try
		{
			out = new ByteArrayOutputStream();
			in = new ByteArrayInputStream(compressed);
			zin = new ZipInputStream(in);
			zin.getNextEntry();
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zin.read(buffer)) != -1)
			{
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString();
		}
		catch (Exception ex)
		{
			decompressed = null;
		}
		finally
		{
			if (zin != null)
			{
				try
				{
					zin.close();
				}
				catch (IOException e)
				{
				}
			}
			if (in != null)
			{
				try
				{
					in.close();
				}
				catch (IOException e)
				{
				}
			}
			if (out != null)
			{
				try
				{
					out.close();
				}
				catch (IOException e)
				{
				}
			}
		}
		return decompressed;
	}

	public static byte[] compressBytesByDeflater(byte input[], boolean nowrap)
	{
		byte output[] = new byte[0];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(
						Deflater.DEFAULT_COMPRESSION, nowrap));
		try
		{
			dos.write(input);
			dos.finish();
			output = baos.toByteArray();
		}
		catch (Exception ex)
		{
		}
		finally
		{
			try
			{
				dos.close();
				baos.close();
			}
			catch (Exception ex)
			{
			}
		}
		return output;
	}

	public static byte[] decompressBytesByInflater(byte input[], boolean nowrap)
	{
		byte output[] = new byte[0];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		InflaterOutputStream ios = new InflaterOutputStream(baos, new Inflater(nowrap));
		try
		{
			ios.write(input);
			ios.finish();
			output = baos.toByteArray();
		}
		catch (Exception ex)
		{
		}
		finally
		{
			try
			{
				ios.close();
				baos.close();
			}
			catch (IOException e)
			{
			}
		}
		return output;
	}
}
